<?php
require("./include/fpdf/fpdf.php");

class PDF extends FPDF
{
	//Page header
	function Header()
	{
		global $header, $w,$title;
		//Logo
	    //$this->Image('logo_pb.png',10,8,33);
	    //Arial bold 15
	    $this->SetFont('Arial','B',13);
	    //Move to the right
	    //$this->Cell(80);
	    //Title
	    $this->Cell(0,10,$title,0,0,'C');
	    //Line break
	    $this->Ln(15);
	    
	    // If table is needed 
	    if($header !=''){
		    //Colors, line width and bold font
		    $this->SetFillColor(255,0,0);
		    $this->SetTextColor(255);
		    $this->SetDrawColor(128,0,0);
		    $this->SetLineWidth(.3);
		    $this->SetFont('Arial','B',10);
		    //Header
		    for($i=0;$i<count($header);$i++)
		        $this->Cell($w[$i],7,$header[$i],1,0,'C',true);
		    $this->Ln();
	    }
	}
	
	//Page footer
	function Footer()
	{
	    //Position at 1.5 cm from bottom
	    $this->SetY(-15);
	    //Arial italic 8
	    $this->SetFont('Arial','I',8);
	    //Page number
	    $this->Cell(0,10,'Cetakan pada '.date("d/m/Y H:i:s").'    Page '.$this->PageNo().'/{nb}',0,0,'C');
	}
	
	//Colored table
	function FancyTable($header,$data)
	{
	    global $w;
		//Colors, line width and bold font
	    /*
		$this->SetFillColor(255,0,0);
	    $this->SetTextColor(255);
	    $this->SetDrawColor(128,0,0);
	    $this->SetLineWidth(.3);
	    $this->SetFont('','B');
	    //Header
	    $w=array(20,55,40,45);
	    for($i=0;$i<count($header);$i++)
	        $this->Cell($w[$i],7,$header[$i],1,0,'C',true);
	    $this->Ln();
	    */
		//$w=array(20,55,40,45);
	    //Color and font restoration
	    $this->SetFillColor(224,235,255);
	    $this->SetTextColor(0);
	    $this->SetFont('');
	    //Data
	    $fill=false;
	    $x=0;
	    foreach($data as $row)
	    {
	        if(empty($row[$x])) $row[$x]='--nil--';
			$this->Cell($w[$x],8,ucwords($row[$x]),'LR',0,'L',$fill);
	        $x++;
	        if($x == sizeof($row)){
	        	$x=0;
		        $this->Ln();
		        $fill=!$fill;
	        }
	    }
	    //$this->Cell(array_sum($w),0,'','T');
	    $this->Cell(0,0,'','T');
	}	
}

//Instanciation of inherited class
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage('L');
$pdf->SetFont('Times','',9.5);

// Set the fields from DB == table header ....
$fields = $header;
/// NoAhli|Nama|NoKPBaru|NoKPLama|Bangsa|Status|Cawangan|Telefon|TelPejabat|
$i=0;

$data = array();
// fetch all data into array ...
while($d = $res->FetchNextObject()){
    $data_arr = array();
    foreach($fields as $f){
    	$data_arr[] = preg_replace("/,/","\n",$d->$f);
    }
    $data[] = $data_arr;
}
$pdf->FancyTable($header=$fields,$data);

$pdf->Output();
?>